package com.yksj.healthtalk.ui.doctorstation;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.DoctorStationGroupListAdapter;
import com.yksj.healthtalk.adapter.SalonAdapter.onClickSalonHeadListener;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.chatting.ChatActivity;
import com.yksj.healthtalk.ui.salon.SalonNewActivity;
import com.yksj.healthtalk.ui.salon.SalonReadSelf;
import com.yksj.healthtalk.ui.salon.SalonSelectPaymentOptionActivity.OnBuyTicketHandlerListener;
import com.yksj.healthtalk.utils.SalonHttpUtil;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 
 * 医生话题 废弃
 * @author zhao
 */
public class DoctorStationGroupListActivity extends BaseFragmentActivity implements OnClickListener,OnBuyTicketHandlerListener,onClickSalonHeadListener{
	
	PullToRefreshListView mRefreshListView;
	DoctorStationGroupListAdapter mAdapter;
	ListView mListView;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctorstation_list_layout);
		initTitle();
		
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("专业话题");
		titleRightBtn2.setText("创建");
		titleRightBtn2.setTextColor(Color.WHITE);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setBackgroundResource(R.drawable.banner_red);
		mRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_listview);
		mListView = mRefreshListView.getRefreshableView();
		mListView.addFooterView(LayoutInflater.from(this).inflate(R.layout.list_line_layout, null));
		mAdapter = new DoctorStationGroupListAdapter(null,getLayoutInflater(),this);
		mRefreshListView.setAdapter(mAdapter);
		mRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				GroupInfoEntity entity = mAdapter.getItem(--position);
				onSendJoinGoupChat(entity);
			}
		});
		mRefreshListView.setOnRefreshListener(new OnRefreshListener2() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				onRequestData();
			}
			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				onRequestData();
			}
		});
	}
	@Override
	protected void onResume() {
		super.onResume();
		onRequestData();
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2:
			Intent intent=new Intent(this,SalonNewActivity.class);
			intent.putExtra("isDoctorSalon",true);
			startActivity(intent);
			overridePendingTransition(R.anim.anim_enter,R.anim.anim_exit);
			break;
		}
	}
	
	private void onRequestData(){
		new AsyncTask<Void, Void, List<GroupInfoEntity>>() {
			@Override
			protected List<GroupInfoEntity> doInBackground(Void... params) {
				final List<GroupInfoEntity> entities = new ArrayList<GroupInfoEntity>();
				AppData appData = HTalkApplication.getAppData();
				List<String> list = appData.getCreatedGroupIdList();
				ConcurrentHashMap<String,Object> map = appData.cacheInformation;
				for (String id : list) {
					GroupInfoEntity entity = (GroupInfoEntity)map.get(id);
					boolean isSlaon =  entity.isSalon();
					if(isSlaon)entities.add(entity);
				}
				return entities;
			}
			@Override
			protected void onPostExecute(List<GroupInfoEntity> list) {
				mAdapter.mGroupInfoEntities.clear();
				mAdapter.mGroupInfoEntities.addAll(list);
				mAdapter.notifyDataSetChanged();
				mRefreshListView.onRefreshComplete();
			}
		}.execute();
	}
	
	/**
	 * 加入群聊
	 * @param entity
	 */
	private void onSendJoinGoupChat(GroupInfoEntity entity){
		SalonHttpUtil.onItemClick(this,this,getSupportFragmentManager(),entity,false);
	}
	
	/**
	 * 购买门票返回
	 */
	@Override
	public void onTicketHandler(String state, GroupInfoEntity entity) {
		if("1".equals(state)){
			Intent  intent1 = new Intent(this,ChatActivity.class);
			intent1.putExtra(ChatActivity.KEY_PARAME,entity);
			startActivity(intent1);
		}else if("0".equals(state)){
			ToastUtil.showLong(this,"多美币不足");
		}else if("-1".equals(state)){
			ToastUtil.showLong(this,"支付失败");
		}
	}

	@Override
	public void onSalonHeadClick(View v, GroupInfoEntity mSalonName,
			int position) {
		Intent intent = new Intent(getApplicationContext(), SalonReadSelf.class);
		intent.putExtra("PersonInfo", mSalonName);
		startActivity(intent);
		
	}
}
